<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item v-show="!!model.id" label="ID" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input disabled v-decorator="['id']" ></a-input>
        </a-form-item>
        <a-form-item label="广告位" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" v-decorator="['slotId', validatorRules.slotId]" :trigger-change="true" dictCode="ads_slot,name,id" placeholder="请选择广告位"/>
        </a-form-item>
        <a-form-item label="广告" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" v-decorator="['adId', validatorRules.adId]" :trigger-change="true" dictCode="ads_ad,name,id" placeholder="请选择广告"/>
        </a-form-item>
        <a-form-item label="投放类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag
            type="list" v-decorator="['putType', validatorRules.putType]"
            @change="onPutTypeChange"
            :trigger-change="true" dictCode="ads_put_type" placeholder="请选择投放类型"/>
        </a-form-item>
        <a-form-item label="开始时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-date placeholder="请选择开始时间" v-decorator="['startTime', validatorRules.startTime]" :trigger-change="true" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="结束时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-date placeholder="请选择结束时间" v-decorator="['endTime', validatorRules.endTime]" :trigger-change="true" style="width: 100%"/>
        </a-form-item>
        <a-form-item v-show="model.putType===2" label="展示数不超过" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number v-decorator="['showMax', validatorRules.showMax]" placeholder="请输入展示数不超过" style="width: 100%"/>
        </a-form-item>
        <a-form-item v-show="model.putType===3" label="点击数不超过" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number v-decorator="['clickMax', validatorRules.clickMax]" placeholder="请输入点击数不超过" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="是否统计点击量" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag :disabled="model.putType===3" type="list" v-decorator="['clickCountable', validatorRules.clickCountable]" :trigger-change="true" dictCode="yn" placeholder="请选择是否统计点击量"/>
        </a-form-item>
        <a-form-item label="投放权重" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number v-decorator="['weight', validatorRules.weight]" placeholder="请输入投放权重" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="每人每天投放次数" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number v-decorator="['dayTimes', validatorRules.dayTimes]" placeholder="请输入每人每天投放次数" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="启用" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" v-decorator="['enabled', validatorRules.enabled]" :trigger-change="true" dictCode="yn" placeholder="请选择启用"/>
        </a-form-item>
        <a-form-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['remark']" placeholder="请输入备注"></a-input>
        </a-form-item>

        <a-form-item label="创建者" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input disabled v-decorator="['createBy']" ></a-input>
        </a-form-item>
        <a-form-item label="创建时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input disabled v-decorator="['createTime']" :trigger-change="true" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="修改者" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input disabled v-decorator="['updateBy']"></a-input>
        </a-form-item>
        <a-form-item label="修改时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input disabled v-decorator="['updateTime']" />
        </a-form-item>

        <a-form-item label="展示数" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input disabled v-decorator="['showCount']"/>
        </a-form-item>
        <a-form-item label="点击数" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input disabled v-decorator="['clickCount']" />
        </a-form-item>
        <a-form-item label="当天日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-date disabled v-decorator="['currDate']"/>
        </a-form-item>
        <a-form-item label="当天展示数" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input disabled v-decorator="['currShowCount']" />
        </a-form-item>

      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>

  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import JDate from '@/components/jeecg/JDate'
  import JDictSelectTag from "@/components/dict/JDictSelectTag"

  export default {
    name: "PuttingModal",
    components: {
      JDate,
      JDictSelectTag,
    },
    data () {
      return {
        form: this.$form.createForm(this),
        title:"操作",
        width:800,
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          slotId: {
            rules: [
              { required: true, message: '请输入广告位!'},
            ]
          },
          adId: {
            rules: [
              { required: true, message: '请输入广告!'},
            ]
          },
          putType: {
            rules: [
              { required: true, message: '请输入投放类型!'},
            ]
          },
          startTime: {
            rules: [
              { required: true, message: '请输入开始时间!'},
            ]
          },
          endTime: {
            rules: [
              { required: true, message: '请输入结束时间!'},
            ]
          },
          showMax: {
            rules: [
              { required: true, message: '请输入展示数不超过!'},
              { pattern: /^-?\d+$/, message: '请输入整数!'},
            ]
          },
          clickMax: {
            rules: [
              { required: true, message: '请输入点击数不超过!'},
              { pattern: /^-?\d+$/, message: '请输入整数!'},
            ]
          },
          clickCountable: {
            rules: [
              { required: true, message: '请输入是否统计点击量!'},
            ]
          },
          weight: {
            rules: [
              { required: true, message: '请输入投放权重!'},
              { pattern: /^-?\d+$/, message: '请输入整数!'},
            ]
          },
          dayTimes: {
            rules: [
              { required: true, message: '请输入每人每天投放次数!'},
            ]
          },
          enabled: {
            rules: [
              { required: true, message: '请输入启用!'},
            ]
          },
        },
        url: {
          add: "/putting/add",
          edit: "/putting/edit",
        }
      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({showMax:0, clickMax:0, clickCountable:1, weight:100, dayTimes:0, enabled:0});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'id','slotId','adId','putType','startTime','endTime',
            'showMax','clickMax','clickCountable','weight','dayTimes','enabled','remark',
            'createBy','createTime','updateBy','updateTime', 'showCount','clickCount','currDate','currShowCount'))
        })
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }

        })
      },
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'id','slotId','adId','putType',
          'startTime','endTime','showMax','clickMax','clickCountable','weight','dayTimes','enabled','remark',
          'createBy','createTime','updateBy','updateTime', 'showCount','clickCount','currDate','currShowCount'))
      },

      onPutTypeChange(val) {
        this.model.putType = Number(val);
        //按点击数（到达点击数量后结束）
        if (this.model.putType===3) {
          //this.form.setFieldsValue({clickCountable:1})
        }
      },
    }
  }
</script>

<style lang="less" scoped>
  .ant-row.ant-form-item {
    margin-bottom: 12px;
  }
</style>